<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link href="../Styles/style.css" rel="stylesheet" type="text/css"/>


</head>

<body>
<div class="article-body">

    <div class="article-intro" id="content">

        <h1>Python3 <span class="color_h1">运算符</span></h1>

        <h2>什么是运算符？</h2>
        <p>本章节主要说明Python的运算符。举个简单的例子 <strong> 4 +5 = 9 </strong>。
            例子中，<strong>4</strong> 和 <strong>5</strong> 被称为<strong>操作数</strong>，"<strong>+</strong>" 称为运算符。</p>
        <p>Python语言支持以下类型的运算符:</p>
        <ul>
            <li><a href="#ysf1">算术运算符</a></li>
            <li><a href="#ysf2">比较（关系）运算符</a></li>
            <li><a href="#ysf3">赋值运算符</a></li>
            <li><a href="#ysf4">逻辑运算符</a></li>
            <li><a href="#ysf5">位运算符</a></li>
            <li><a href="#ysf6">成员运算符</a></li>
            <li><a href="#ysf7">身份运算符</a></li>
            <li><a href="#ysf8">运算符优先级</a></li>
        </ul>
        <p>接下来让我们一个个来学习Python的运算符。</p>

        <h2 id="ysf1">Python算术运算符</h2>
        <p>以下假设变量a为10，变量b为21：</p>
        <table class="reference">
            <tbody>
            <tr>
                <th>运算符</th>
                <th>描述</th>
                <th>实例</th>
            </tr>
            <tr>
                <td>+</td>
                <td>加 - 两个对象相加</td>
                <td> a + b 输出结果 31</td>
            </tr>
            <tr>
                <td>-</td>
                <td>减 - 得到负数或是一个数减去另一个数</td>
                <td> a - b 输出结果 -11</td>
            </tr>
            <tr>
                <td>*</td>
                <td>乘 - 两个数相乘或是返回一个被重复若干次的字符串</td>
                <td> a * b 输出结果 210</td>
            </tr>
            <tr>
                <td>/</td>
                <td>除 - x 除以 y</td>
                <td> b / a 输出结果 2.1</td>
            </tr>
            <tr>
                <td>%</td>
                <td>取模 - 返回除法的余数</td>
                <td> b % a 输出结果 1</td>
            </tr>
            <tr>
                <td>**</td>
                <td>幂 - 返回x的y次幂</td>
                <td> a**b 为10的21次方</td>
            </tr>
            <tr>
                <td>//</td>
                <td>取整除 - 返回商的整数部分</td>
                <td> 9//2 输出结果 4 , 9.0//2.0 输出结果 4.0</td>
            </tr>
            </tbody>
        </table>

        <p>以下实例演示了Python所有算术运算符的操作：</p>

        <div class="example">
            <div class="example">实例(Python 3.0+)</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-comment">#!/usr/bin/python3</span><span class="hl-code">

</span><span class="hl-identifier">a</span><span class="hl-code"> = </span><span class="hl-number">21</span><span
                        class="hl-code">
</span><span class="hl-identifier">b</span><span class="hl-code"> = </span><span class="hl-number">10</span><span
                        class="hl-code">
</span><span class="hl-identifier">c</span><span class="hl-code"> = </span><span class="hl-number">0</span><span
                        class="hl-code">

</span><span class="hl-identifier">c</span><span class="hl-code"> = </span><span class="hl-identifier">a</span><span
                        class="hl-code"> + </span><span class="hl-identifier">b</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">1 - c 的值为：</span><span
                        class="hl-quotes">"</span><span class="hl-code">, </span><span
                        class="hl-identifier">c</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-identifier">c</span><span class="hl-code"> = </span><span class="hl-identifier">a</span><span
                        class="hl-code"> - </span><span class="hl-identifier">b</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">2 - c 的值为：</span><span
                        class="hl-quotes">"</span><span class="hl-code">, </span><span
                        class="hl-identifier">c</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-identifier">c</span><span class="hl-code"> = </span><span class="hl-identifier">a</span><span
                        class="hl-code"> * </span><span class="hl-identifier">b</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">3 - c 的值为：</span><span
                        class="hl-quotes">"</span><span class="hl-code">, </span><span
                        class="hl-identifier">c</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-identifier">c</span><span class="hl-code"> = </span><span class="hl-identifier">a</span><span
                        class="hl-code"> / </span><span class="hl-identifier">b</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">4 - c 的值为：</span><span
                        class="hl-quotes">"</span><span class="hl-code">, </span><span
                        class="hl-identifier">c</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-identifier">c</span><span class="hl-code"> = </span><span class="hl-identifier">a</span><span
                        class="hl-code"> % </span><span class="hl-identifier">b</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">5 - c 的值为：</span><span
                        class="hl-quotes">"</span><span class="hl-code">, </span><span
                        class="hl-identifier">c</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-comment"># 修改变量 a 、b 、c</span><span class="hl-code">
</span><span class="hl-identifier">a</span><span class="hl-code"> = </span><span class="hl-number">2</span><span
                        class="hl-code">
</span><span class="hl-identifier">b</span><span class="hl-code"> = </span><span class="hl-number">3</span><span
                        class="hl-code">
</span><span class="hl-identifier">c</span><span class="hl-code"> = </span><span class="hl-identifier">a</span><span
                        class="hl-code">**</span><span class="hl-identifier">b</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">6 - c 的值为：</span><span
                        class="hl-quotes">"</span><span class="hl-code">, </span><span
                        class="hl-identifier">c</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-identifier">a</span><span class="hl-code"> = </span><span class="hl-number">10</span><span
                        class="hl-code">
</span><span class="hl-identifier">b</span><span class="hl-code"> = </span><span class="hl-number">5</span><span
                        class="hl-code">
</span><span class="hl-identifier">c</span><span class="hl-code"> = </span><span class="hl-identifier">a</span><span
                        class="hl-code">//</span><span class="hl-identifier">b</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">7 - c 的值为：</span><span
                        class="hl-quotes">"</span><span class="hl-code">, </span><span
                        class="hl-identifier">c</span><span class="hl-brackets">)</span></pre>
            </div>
        </div>
        <p>以上实例输出结果：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="lit">1</span><span class="pln"> </span><span
                class="pun">-</span><span class="pln"> c </span><span class="pun">的值为：</span><span
                class="pln"> </span><span class="lit">31</span><span class="pln">
</span><span class="lit">2</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> c </span><span
                class="pun">的值为：</span><span class="pln"> </span><span class="lit">11</span><span class="pln">
</span><span class="lit">3</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> c </span><span
                class="pun">的值为：</span><span class="pln"> </span><span class="lit">210</span><span class="pln">
</span><span class="lit">4</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> c </span><span
                class="pun">的值为：</span><span class="pln"> </span><span class="lit">2.1</span><span class="pln">
</span><span class="lit">5</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> c </span><span
                class="pun">的值为：</span><span class="pln"> </span><span class="lit">1</span><span class="pln">
</span><span class="lit">6</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> c </span><span
                class="pun">的值为：</span><span class="pln"> </span><span class="lit">8</span><span class="pln">
</span><span class="lit">7</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> c </span><span
                class="pun">的值为：</span><span class="pln"> </span><span class="lit">2</span></pre>


        <h2 id="ysf2">Python比较运算符</h2>
        <p>以下假设变量a为10，变量b为20：</p>
        <table class="reference">
            <tbody>
            <tr>
                <th width="10%">运算符</th>
                <th>描述</th>
                <th>实例</th>
            </tr>
            <tr>
                <td>==</td>
                <td> 等于 - 比较对象是否相等</td>
                <td> (a == b) 返回 False。</td>
            </tr>
            <tr>
                <td>!=</td>
                <td> 不等于 - 比较两个对象是否不相等</td>
                <td> (a != b) 返回 True。</td>
            </tr>

            <tr>
                <td>&gt;</td>
                <td> 大于 - 返回x是否大于y</td>
                <td> (a &gt; b) 返回 False。</td>
            </tr>
            <tr>
                <td>&lt;</td>
                <td> 小于 - 返回x是否小于y。所有比较运算符返回1表示真，返回0表示假。这分别与特殊的变量True和False等价。注意，这些变量名的大写。</td>
                <td> (a &lt; b) 返回 True。</td>
            </tr>
            <tr>
                <td>&gt;=</td>
                <td> 大于等于 - 返回x是否大于等于y。</td>
                <td> (a &gt;= b) 返回 False。</td>

            </tr>
            <tr>
                <td>&lt;=</td>
                <td> 小于等于 - 返回x是否小于等于y。</td>
                <td> (a &lt;= b) 返回 True。</td>
            </tr>
            </tbody>
        </table>
        <p>以下实例演示了Python所有比较运算符的操作：</p>
        <div class="example">
            <div class="example">实例(Python 3.0+)</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-comment">#!/usr/bin/python3</span><span class="hl-code">

</span><span class="hl-identifier">a</span><span class="hl-code"> = </span><span class="hl-number">21</span><span
                        class="hl-code">
</span><span class="hl-identifier">b</span><span class="hl-code"> = </span><span class="hl-number">10</span><span
                        class="hl-code">
</span><span class="hl-identifier">c</span><span class="hl-code"> = </span><span class="hl-number">0</span><span
                        class="hl-code">

</span><span class="hl-identifier">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-code"> </span><span class="hl-identifier">a</span><span
                        class="hl-code"> == </span><span class="hl-identifier">b</span><span
                        class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">1 - a 等于 b</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-reserved">else</span><span class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">1 - a 不等于 b</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-identifier">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-code"> </span><span class="hl-identifier">a</span><span
                        class="hl-code"> != </span><span class="hl-identifier">b</span><span
                        class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">2 - a 不等于 b</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-reserved">else</span><span class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">2 - a 等于 b</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-identifier">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-code"> </span><span class="hl-identifier">a</span><span
                        class="hl-code"> &lt; </span><span class="hl-identifier">b</span><span
                        class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">3 - a 小于 b</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-reserved">else</span><span class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">3 - a 大于等于 b</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-identifier">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-code"> </span><span class="hl-identifier">a</span><span
                        class="hl-code"> &gt; </span><span class="hl-identifier">b</span><span
                        class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">4 - a 大于 b</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-reserved">else</span><span class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">4 - a 小于等于 b</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-comment"># 修改变量 a 和 b 的值</span><span class="hl-code">
</span><span class="hl-identifier">a</span><span class="hl-code"> = </span><span class="hl-number">5</span><span
                        class="hl-code">;
</span><span class="hl-identifier">b</span><span class="hl-code"> = </span><span class="hl-number">20</span><span
                        class="hl-code">;
</span><span class="hl-identifier">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code"> &lt;= </span><span
                        class="hl-identifier">b</span><span class="hl-code"> </span><span
                        class="hl-brackets">)</span><span class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">5 - a 小于等于 b</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-reserved">else</span><span class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">5 - a 大于  b</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-identifier">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-code"> </span><span class="hl-identifier">b</span><span class="hl-code"> &gt;= </span><span
                        class="hl-identifier">a</span><span class="hl-code"> </span><span
                        class="hl-brackets">)</span><span class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">6 - b 大于等于 a</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-reserved">else</span><span class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">6 - b 小于 a</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span></pre>
            </div>
        </div>

        <p>以上实例输出结果：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="lit">1</span><span class="pln"> </span><span
                class="pun">-</span><span class="pln"> a </span><span class="pun">不等于</span><span class="pln"> b
</span><span class="lit">2</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> a </span><span
                class="pun">不等于</span><span class="pln"> b
</span><span class="lit">3</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> a </span><span
                class="pun">大于等于</span><span class="pln"> b
</span><span class="lit">4</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> a </span><span
                class="pun">大于</span><span class="pln"> b
</span><span class="lit">5</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> a </span><span
                class="pun">小于等于</span><span class="pln"> b
</span><span class="lit">6</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> b </span><span
                class="pun">大于等于</span><span class="pln"> a</span></pre>

        <h2 id="ysf3">Python赋值运算符</h2>
        <p>以下假设变量a为10，变量b为20：</p>
        <table class="reference">
            <tbody>
            <tr>
                <th>运算符</th>
                <th>描述</th>
                <th>实例</th>
            </tr>
            <tr>
                <td>=</td>
                <td>简单的赋值运算符</td>
                <td> c = a + b 将 a + b 的运算结果赋值为 c</td>
            </tr>
            <tr>
                <td>+=</td>
                <td>加法赋值运算符</td>
                <td> c += a 等效于 c = c + a</td>
            </tr>
            <tr>
                <td>-=</td>
                <td>减法赋值运算符</td>
                <td> c -= a 等效于 c = c - a</td>
            </tr>
            <tr>
                <td>*=</td>
                <td>乘法赋值运算符</td>
                <td> c *= a 等效于 c = c * a</td>
            </tr>
            <tr>
                <td>/=</td>
                <td>除法赋值运算符</td>
                <td> c /= a 等效于 c = c / a</td>
            </tr>
            <tr>
                <td>%=</td>
                <td>取模赋值运算符</td>
                <td> c %= a 等效于 c = c % a</td>
            </tr>
            <tr>
                <td>**=</td>
                <td>幂赋值运算符</td>
                <td> c **= a 等效于 c = c ** a</td>
            </tr>
            <tr>
                <td>//=</td>
                <td> 取整除赋值运算符</td>
                <td> c //= a 等效于 c = c // a</td>
            </tr>
            </tbody>
        </table>
        <p>以下实例演示了Python所有赋值运算符的操作：</p>
        <div class="example">
            <div class="example">实例(Python 3.0+)</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-comment">#!/usr/bin/python3</span><span class="hl-code">

</span><span class="hl-identifier">a</span><span class="hl-code"> = </span><span class="hl-number">21</span><span
                        class="hl-code">
</span><span class="hl-identifier">b</span><span class="hl-code"> = </span><span class="hl-number">10</span><span
                        class="hl-code">
</span><span class="hl-identifier">c</span><span class="hl-code"> = </span><span class="hl-number">0</span><span
                        class="hl-code">

</span><span class="hl-identifier">c</span><span class="hl-code"> = </span><span class="hl-identifier">a</span><span
                        class="hl-code"> + </span><span class="hl-identifier">b</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">1 - c 的值为：</span><span
                        class="hl-quotes">"</span><span class="hl-code">, </span><span
                        class="hl-identifier">c</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-identifier">c</span><span class="hl-code"> += </span><span class="hl-identifier">a</span><span
                        class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">2 - c 的值为：</span><span
                        class="hl-quotes">"</span><span class="hl-code">, </span><span
                        class="hl-identifier">c</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-identifier">c</span><span class="hl-code"> *= </span><span class="hl-identifier">a</span><span
                        class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">3 - c 的值为：</span><span
                        class="hl-quotes">"</span><span class="hl-code">, </span><span
                        class="hl-identifier">c</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-identifier">c</span><span class="hl-code"> /= </span><span class="hl-identifier">a</span><span
                        class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">4 - c 的值为：</span><span
                        class="hl-quotes">"</span><span class="hl-code">, </span><span
                        class="hl-identifier">c</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-identifier">c</span><span class="hl-code"> = </span><span class="hl-number">2</span><span
                        class="hl-code">
</span><span class="hl-identifier">c</span><span class="hl-code"> %= </span><span class="hl-identifier">a</span><span
                        class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">5 - c 的值为：</span><span
                        class="hl-quotes">"</span><span class="hl-code">, </span><span
                        class="hl-identifier">c</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-identifier">c</span><span class="hl-code"> **= </span><span class="hl-identifier">a</span><span
                        class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">6 - c 的值为：</span><span
                        class="hl-quotes">"</span><span class="hl-code">, </span><span
                        class="hl-identifier">c</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-identifier">c</span><span class="hl-code"> //= </span><span class="hl-identifier">a</span><span
                        class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">7 - c 的值为：</span><span
                        class="hl-quotes">"</span><span class="hl-code">, </span><span
                        class="hl-identifier">c</span><span class="hl-brackets">)</span></pre>
            </div>
        </div>
        <p>以上实例输出结果：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="lit">1</span><span class="pln"> </span><span
                class="pun">-</span><span class="pln"> c </span><span class="pun">的值为：</span><span
                class="pln"> </span><span class="lit">31</span><span class="pln">
</span><span class="lit">2</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> c </span><span
                class="pun">的值为：</span><span class="pln"> </span><span class="lit">52</span><span class="pln">
</span><span class="lit">3</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> c </span><span
                class="pun">的值为：</span><span class="pln"> </span><span class="lit">1092</span><span class="pln">
</span><span class="lit">4</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> c </span><span
                class="pun">的值为：</span><span class="pln"> </span><span class="lit">52.0</span><span class="pln">
</span><span class="lit">5</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> c </span><span
                class="pun">的值为：</span><span class="pln"> </span><span class="lit">2</span><span class="pln">
</span><span class="lit">6</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> c </span><span
                class="pun">的值为：</span><span class="pln"> </span><span class="lit">2097152</span><span class="pln">
</span><span class="lit">7</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> c </span><span
                class="pun">的值为：</span><span class="pln"> </span><span class="lit">99864</span></pre>

        <h2 id="ysf5">Python位运算符</h2>
        <p>
            按位运算符是把数字看作二进制来进行计算的。Python中的按位运算法则如下：</p>
        <p>下表中变量 a 为 60，b 为 13二进制格式如下：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">a </span><span class="pun">=</span><span
                class="pln"> </span><span class="lit">0011</span><span class="pln"> </span><span class="lit">1100</span><span
                class="pln">

b </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0000</span><span
                class="pln"> </span><span class="lit">1101</span><span class="pln">

</span><span class="pun">-----------------</span><span class="pln">

a</span><span class="pun">&amp;</span><span class="pln">b </span><span class="pun">=</span><span
                class="pln"> </span><span class="lit">0000</span><span class="pln"> </span><span class="lit">1100</span><span
                class="pln">

a</span><span class="pun">|</span><span class="pln">b </span><span class="pun">=</span><span class="pln"> </span><span
                class="lit">0011</span><span class="pln"> </span><span class="lit">1101</span><span class="pln">

a</span><span class="pun">^</span><span class="pln">b </span><span class="pun">=</span><span class="pln"> </span><span
                class="lit">0011</span><span class="pln"> </span><span class="lit">0001</span><span class="pln">

</span><span class="pun">~</span><span class="pln">a  </span><span class="pun">=</span><span class="pln"> </span><span
                class="lit">1100</span><span class="pln"> </span><span class="lit">0011</span></pre>
        <table class="reference">
            <tbody>
            <tr>
                <th>运算符</th>
                <th>描述</th>
                <th>实例</th>
            </tr>
            <tr>
                <td>&amp;</td>
                <td>按位与运算符：参与运算的两个值,如果两个相应位都为1,则该位的结果为1,否则为0</td>
                <td> (a &amp; b) 输出结果 12 ，二进制解释： 0000 1100</td>
            </tr>
            <tr>
                <td>|</td>
                <td> 按位或运算符：只要对应的二个二进位有一个为1时，结果位就为1。</td>
                <td> (a | b) 输出结果 61 ，二进制解释： 0011 1101</td>
            </tr>
            <tr>
                <td>^</td>
                <td>按位异或运算符：当两对应的二进位相异时，结果为1</td>
                <td> (a ^ b) 输出结果 49 ，二进制解释： 0011 0001</td>
            </tr>
            <tr>
                <td>~</td>
                <td> 按位取反运算符：对数据的每个二进制位取反,即把1变为0,把0变为1。<span class="marked">~x</span> 类似于 <span
                        class="marked">-x-1</span></td>
                <td> (~a ) 输出结果 -61 ，二进制解释： 1100 0011， 在一个有符号二进制数的补码形式。</td>
            </tr>
            <tr>
                <td>&lt;&lt;</td>
                <td>左移动运算符：运算数的各二进位全部左移若干位，由"&lt;&lt;"右边的数指定移动的位数，高位丢弃，低位补0。</td>
                <td> a &lt;&lt; 2 输出结果 240 ，二进制解释： 1111 0000</td>
            </tr>
            <tr>
                <td>&gt;&gt;</td>
                <td>右移动运算符：把"&gt;&gt;"左边的运算数的各二进位全部右移若干位，"&gt;&gt;"右边的数指定移动的位数</td>
                <td> a &gt;&gt; 2 输出结果 15 ，二进制解释： 0000 1111</td>
            </tr>
            </tbody>
        </table>
        <p>以下实例演示了Python所有位运算符的操作：</p>
        <div class="example">
            <div class="example">实例(Python 3.0+)</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-comment">#!/usr/bin/python3</span><span class="hl-code">

</span><span class="hl-identifier">a</span><span class="hl-code"> = </span><span class="hl-number">60</span><span
                        class="hl-code">            </span><span class="hl-comment"># 60 = 0011 1100 </span><span
                        class="hl-code">
</span><span class="hl-identifier">b</span><span class="hl-code"> = </span><span class="hl-number">13</span><span
                        class="hl-code">            </span><span class="hl-comment"># 13 = 0000 1101 </span><span
                        class="hl-code">
</span><span class="hl-identifier">c</span><span class="hl-code"> = </span><span class="hl-number">0</span><span
                        class="hl-code">

</span><span class="hl-identifier">c</span><span class="hl-code"> = </span><span class="hl-identifier">a</span><span
                        class="hl-code"> &amp; </span><span class="hl-identifier">b</span><span
                        class="hl-code">;        </span><span class="hl-comment"># 12 = 0000 1100</span><span
                        class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">1 - c 的值为：</span><span
                        class="hl-quotes">"</span><span class="hl-code">, </span><span
                        class="hl-identifier">c</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-identifier">c</span><span class="hl-code"> = </span><span class="hl-identifier">a</span><span
                        class="hl-code"> | </span><span class="hl-identifier">b</span><span
                        class="hl-code">;        </span><span class="hl-comment"># 61 = 0011 1101 </span><span
                        class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">2 - c 的值为：</span><span
                        class="hl-quotes">"</span><span class="hl-code">, </span><span
                        class="hl-identifier">c</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-identifier">c</span><span class="hl-code"> = </span><span class="hl-identifier">a</span><span
                        class="hl-code"> ^ </span><span class="hl-identifier">b</span><span
                        class="hl-code">;        </span><span class="hl-comment"># 49 = 0011 0001</span><span
                        class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">3 - c 的值为：</span><span
                        class="hl-quotes">"</span><span class="hl-code">, </span><span
                        class="hl-identifier">c</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-identifier">c</span><span class="hl-code"> = ~</span><span class="hl-identifier">a</span><span
                        class="hl-code">;           </span><span class="hl-comment"># -61 = 1100 0011</span><span
                        class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">4 - c 的值为：</span><span
                        class="hl-quotes">"</span><span class="hl-code">, </span><span
                        class="hl-identifier">c</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-identifier">c</span><span class="hl-code"> = </span><span class="hl-identifier">a</span><span
                        class="hl-code"> &lt;&lt; </span><span class="hl-number">2</span><span
                        class="hl-code">;       </span><span class="hl-comment"># 240 = 1111 0000</span><span
                        class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">5 - c 的值为：</span><span
                        class="hl-quotes">"</span><span class="hl-code">, </span><span
                        class="hl-identifier">c</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-identifier">c</span><span class="hl-code"> = </span><span class="hl-identifier">a</span><span
                        class="hl-code"> &gt;&gt; </span><span class="hl-number">2</span><span
                        class="hl-code">;       </span><span class="hl-comment"># 15 = 0000 1111</span><span
                        class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">6 - c 的值为：</span><span
                        class="hl-quotes">"</span><span class="hl-code">, </span><span
                        class="hl-identifier">c</span><span class="hl-brackets">)</span></pre>
            </div>
        </div>
        <p>以上实例输出结果：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="lit">1</span><span class="pln"> </span><span
                class="pun">-</span><span class="pln"> c </span><span class="pun">的值为：</span><span
                class="pln"> </span><span class="lit">12</span><span class="pln">
</span><span class="lit">2</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> c </span><span
                class="pun">的值为：</span><span class="pln"> </span><span class="lit">61</span><span class="pln">
</span><span class="lit">3</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> c </span><span
                class="pun">的值为：</span><span class="pln"> </span><span class="lit">49</span><span class="pln">
</span><span class="lit">4</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> c </span><span
                class="pun">的值为：</span><span class="pln"> </span><span class="pun">-</span><span
                class="lit">61</span><span class="pln">
</span><span class="lit">5</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> c </span><span
                class="pun">的值为：</span><span class="pln"> </span><span class="lit">240</span><span class="pln">
</span><span class="lit">6</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> c </span><span
                class="pun">的值为：</span><span class="pln"> </span><span class="lit">15</span></pre>

        <h2 id="ysf4">Python逻辑运算符</h2>
        <p>Python语言支持逻辑运算符，以下假设变量 a 为 10, b为 20:</p>

        <table class="reference">
            <tbody>
            <tr>
                <th>运算符</th>
                <th>逻辑表达式</th>
                <th>描述</th>
                <th>实例</th>
            </tr>
            <tr>
                <td>and</td>
                <td>x and y</td>
                <td> 布尔"与" - 如果 x 为 False，x and y 返回 False，否则它返回 y 的计算值。</td>
                <td> (a and b) 返回 20。</td>
            </tr>
            <tr>
                <td>or</td>
                <td>x or y</td>
                <td>布尔"或" - 如果 x 是 True，它返回 x 的值，否则它返回 y 的计算值。</td>
                <td> (a or b) 返回 10。</td>
            </tr>
            <tr>
                <td>not</td>
                <td>not x</td>
                <td>布尔"非" - 如果 x 为 True，返回 False 。如果 x 为 False，它返回 True。</td>
                <td> not(a and b) 返回 False</td>
            </tr>
            </tbody>
        </table>
        <p>以上实例输出结果：</p>
        <div class="example">
            <div class="example">实例(Python 3.0+)</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-comment">#!/usr/bin/python3</span><span class="hl-code">

</span><span class="hl-identifier">a</span><span class="hl-code"> = </span><span class="hl-number">10</span><span
                        class="hl-code">
</span><span class="hl-identifier">b</span><span class="hl-code"> = </span><span class="hl-number">20</span><span
                        class="hl-code">

</span><span class="hl-identifier">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code"> </span><span
                        class="hl-reserved">and</span><span class="hl-code"> </span><span class="hl-identifier">b</span><span
                        class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">1 - 变量 a 和 b 都为 true</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-reserved">else</span><span class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">1 - 变量 a 和 b 有一个不为 true</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-identifier">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code"> </span><span
                        class="hl-reserved">or</span><span class="hl-code"> </span><span
                        class="hl-identifier">b</span><span class="hl-code"> </span><span
                        class="hl-brackets">)</span><span class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">2 - 变量 a 和 b 都为 true，或其中一个变量为 true</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-reserved">else</span><span class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">2 - 变量 a 和 b 都不为 true</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-comment"># 修改变量 a 的值</span><span class="hl-code">
</span><span class="hl-identifier">a</span><span class="hl-code"> = </span><span class="hl-number">0</span><span
                        class="hl-code">
</span><span class="hl-identifier">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code"> </span><span
                        class="hl-reserved">and</span><span class="hl-code"> </span><span class="hl-identifier">b</span><span
                        class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">3 - 变量 a 和 b 都为 true</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-reserved">else</span><span class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">3 - 变量 a 和 b 有一个不为 true</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-identifier">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code"> </span><span
                        class="hl-reserved">or</span><span class="hl-code"> </span><span
                        class="hl-identifier">b</span><span class="hl-code"> </span><span
                        class="hl-brackets">)</span><span class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">4 - 变量 a 和 b 都为 true，或其中一个变量为 true</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-reserved">else</span><span class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">4 - 变量 a 和 b 都不为 true</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-identifier">not</span><span
                        class="hl-brackets">(</span><span class="hl-code"> </span><span
                        class="hl-identifier">a</span><span class="hl-code"> </span><span class="hl-reserved">and</span><span
                        class="hl-code"> </span><span class="hl-identifier">b</span><span class="hl-code"> </span><span
                        class="hl-brackets">)</span><span class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span
                        class="hl-string">5 - 变量 a 和 b 都为 false，或其中一个变量为 false</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-reserved">else</span><span class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">5 - 变量 a 和 b 都为 true</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span></pre>
            </div>
        </div>
        <p>以上实例输出结果：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="lit">1</span><span class="pln"> </span><span
                class="pun">-</span><span class="pln"> </span><span class="pun">变量</span><span
                class="pln"> a </span><span class="pun">和</span><span class="pln"> b </span><span
                class="pun">都为</span><span class="pln"> </span><span class="kwd">true</span><span class="pln">
</span><span class="lit">2</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> </span><span
                class="pun">变量</span><span class="pln"> a </span><span class="pun">和</span><span
                class="pln"> b </span><span class="pun">都为</span><span class="pln"> </span><span class="kwd">true</span><span
                class="pun">，或其中一个变量为</span><span class="pln"> </span><span class="kwd">true</span><span class="pln">
</span><span class="lit">3</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> </span><span
                class="pun">变量</span><span class="pln"> a </span><span class="pun">和</span><span
                class="pln"> b </span><span class="pun">有一个不为</span><span class="pln"> </span><span
                class="kwd">true</span><span class="pln">
</span><span class="lit">4</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> </span><span
                class="pun">变量</span><span class="pln"> a </span><span class="pun">和</span><span
                class="pln"> b </span><span class="pun">都为</span><span class="pln"> </span><span class="kwd">true</span><span
                class="pun">，或其中一个变量为</span><span class="pln"> </span><span class="kwd">true</span><span class="pln">
</span><span class="lit">5</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> </span><span
                class="pun">变量</span><span class="pln"> a </span><span class="pun">和</span><span
                class="pln"> b </span><span class="pun">都为</span><span class="pln"> </span><span
                class="kwd">false</span><span class="pun">，或其中一个变量为</span><span class="pln"> </span><span class="kwd">false</span></pre>

        <h2 id="ysf6">Python成员运算符</h2>
        <p>除了以上的一些运算符之外，Python还支持成员运算符，测试实例中包含了一系列的成员，包括字符串，列表或元组。</p>
        <table class="reference">
            <tbody>
            <tr>
                <th>运算符</th>
                <th>描述</th>
                <th>实例</th>
            </tr>
            <tr>
                <td>in</td>
                <td>
                    如果在指定的序列中找到值返回 True，否则返回 False。
                </td>
                <td> x 在 y 序列中 , 如果 x 在 y 序列中返回 True。</td>
            </tr>
            <tr>
                <td>not in</td>
                <td>如果在指定的序列中没有找到值返回 True，否则返回 False。</td>
                <td>x 不在 y 序列中 , 如果 x 不在 y 序列中返回 True。</td>
            </tr>
            </tbody>
        </table>
        <p>以下实例演示了Python所有成员运算符的操作：</p>
        <div class="example">
            <div class="example">实例(Python 3.0+)</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-comment">#!/usr/bin/python3</span><span class="hl-code">

</span><span class="hl-identifier">a</span><span class="hl-code"> = </span><span class="hl-number">10</span><span
                        class="hl-code">
</span><span class="hl-identifier">b</span><span class="hl-code"> = </span><span class="hl-number">20</span><span
                        class="hl-code">
</span><span class="hl-identifier">list</span><span class="hl-code"> = </span><span class="hl-brackets">[</span><span
                        class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">2</span><span
                        class="hl-code">, </span><span class="hl-number">3</span><span class="hl-code">, </span><span
                        class="hl-number">4</span><span class="hl-code">, </span><span class="hl-number">5</span><span
                        class="hl-code"> </span><span class="hl-brackets">]</span><span class="hl-code">;

</span><span class="hl-identifier">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code"> </span><span
                        class="hl-reserved">in</span><span class="hl-code"> </span><span
                        class="hl-identifier">list</span><span class="hl-code"> </span><span
                        class="hl-brackets">)</span><span class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">1 - 变量 a 在给定的列表中 list 中</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-reserved">else</span><span class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">1 - 变量 a 不在给定的列表中 list 中</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-identifier">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-code"> </span><span class="hl-identifier">b</span><span class="hl-code"> </span><span
                        class="hl-reserved">not</span><span class="hl-code"> </span><span
                        class="hl-reserved">in</span><span class="hl-code"> </span><span
                        class="hl-identifier">list</span><span class="hl-code"> </span><span
                        class="hl-brackets">)</span><span class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">2 - 变量 b 不在给定的列表中 list 中</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-reserved">else</span><span class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">2 - 变量 b 在给定的列表中 list 中</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-comment"># 修改变量 a 的值</span><span class="hl-code">
</span><span class="hl-identifier">a</span><span class="hl-code"> = </span><span class="hl-number">2</span><span
                        class="hl-code">
</span><span class="hl-identifier">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code"> </span><span
                        class="hl-reserved">in</span><span class="hl-code"> </span><span
                        class="hl-identifier">list</span><span class="hl-code"> </span><span
                        class="hl-brackets">)</span><span class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">3 - 变量 a 在给定的列表中 list 中</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-reserved">else</span><span class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">3 - 变量 a 不在给定的列表中 list 中</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span></pre>
            </div>
        </div>
        <p>以上实例输出结果：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="lit">1</span><span class="pln"> </span><span
                class="pun">-</span><span class="pln"> </span><span class="pun">变量</span><span
                class="pln"> a </span><span class="pun">不在给定的列表中</span><span class="pln"> list </span><span class="pun">中</span><span
                class="pln">
</span><span class="lit">2</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> </span><span
                class="pun">变量</span><span class="pln"> b </span><span class="pun">不在给定的列表中</span><span class="pln"> list </span><span
                class="pun">中</span><span class="pln">
</span><span class="lit">3</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> </span><span
                class="pun">变量</span><span class="pln"> a </span><span class="pun">在给定的列表中</span><span
                class="pln"> list </span><span class="pun">中</span></pre>

        <h2 id="ysf7">Python身份运算符</h2>
        <p>
            身份运算符用于比较两个对象的存储单元</p>
        <table class="reference">
            <tbody>
            <tr>
                <th width="10%">运算符</th>
                <th>描述</th>
                <th>实例</th>
            </tr>
            <tr>
                <td>is</td>
                <td>
                    is 是判断两个标识符是不是引用自一个对象
                </td>
                <td><strong>x is y</strong>, 类似 <strong>id(x) == id(y)</strong> , 如果引用的是同一个对象则返回 True，否则返回 False</td>
            </tr>
            <tr>
                <td>is not</td>
                <td>is not 是判断两个标识符是不是引用自不同对象</td>
                <td><strong> x is not y</strong> ， 类似 <strong>id(a) != id(b)</strong>。如果引用的不是同一个对象则返回结果 True，否则返回 False。
                </td>
            </tr>
            </tbody>
        </table>
        <p><strong>注：</strong> <a href="/python/python-func-id.html" target="_blank">id()</a> 函数用于获取对象内存地址。</p>
        <p>以下实例演示了Python所有身份运算符的操作：</p>
        <div class="example">
            <div class="example">实例(Python 3.0+)</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-comment">#!/usr/bin/python3</span><span class="hl-code">

</span><span class="hl-identifier">a</span><span class="hl-code"> = </span><span class="hl-number">20</span><span
                        class="hl-code">
</span><span class="hl-identifier">b</span><span class="hl-code"> = </span><span class="hl-number">20</span><span
                        class="hl-code">

</span><span class="hl-identifier">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code"> </span><span
                        class="hl-reserved">is</span><span class="hl-code"> </span><span
                        class="hl-identifier">b</span><span class="hl-code"> </span><span
                        class="hl-brackets">)</span><span class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">1 - a 和 b 有相同的标识</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-reserved">else</span><span class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">1 - a 和 b 没有相同的标识</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-identifier">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-code"> </span><span class="hl-builtin">id</span><span
                        class="hl-brackets">(</span><span class="hl-identifier">a</span><span
                        class="hl-brackets">)</span><span class="hl-code"> == </span><span
                        class="hl-builtin">id</span><span class="hl-brackets">(</span><span
                        class="hl-identifier">b</span><span class="hl-brackets">)</span><span
                        class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">2 - a 和 b 有相同的标识</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-reserved">else</span><span class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">2 - a 和 b 没有相同的标识</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-comment"># 修改变量 b 的值</span><span class="hl-code">
</span><span class="hl-identifier">b</span><span class="hl-code"> = </span><span class="hl-number">30</span><span
                        class="hl-code">
</span><span class="hl-identifier">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code"> </span><span
                        class="hl-reserved">is</span><span class="hl-code"> </span><span
                        class="hl-identifier">b</span><span class="hl-code"> </span><span
                        class="hl-brackets">)</span><span class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">3 - a 和 b 有相同的标识</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-reserved">else</span><span class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">3 - a 和 b 没有相同的标识</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-identifier">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code"> </span><span
                        class="hl-reserved">is</span><span class="hl-code"> </span><span
                        class="hl-reserved">not</span><span class="hl-code"> </span><span class="hl-identifier">b</span><span
                        class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">4 - a 和 b 没有相同的标识</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-reserved">else</span><span class="hl-code">:
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">4 - a 和 b 有相同的标识</span><span
                        class="hl-quotes">"</span><span class="hl-brackets">)</span></pre>
            </div>
        </div>
        <p>以上实例输出结果：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="lit">1</span><span class="pln"> </span><span
                class="pun">-</span><span class="pln"> a </span><span class="pun">和</span><span
                class="pln"> b </span><span class="pun">有相同的标识</span><span class="pln">
</span><span class="lit">2</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> a </span><span
                class="pun">和</span><span class="pln"> b </span><span class="pun">有相同的标识</span><span class="pln">
</span><span class="lit">3</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> a </span><span
                class="pun">和</span><span class="pln"> b </span><span class="pun">没有相同的标识</span><span class="pln">
</span><span class="lit">4</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> a </span><span
                class="pun">和</span><span class="pln"> b </span><span class="pun">没有相同的标识</span></pre>
        <blockquote>
            <p>is 与 == 区别：</p>
            <p>is 用于判断两个变量引用对象是否为同一个， == 用于判断引用变量的值是否相等。</p>
            <div class="example">
                <div class="example_code">
                    <pre class="hl-main"><span class="hl-code">&gt;&gt;&gt;</span><span
                            class="hl-identifier">a</span><span class="hl-code"> = </span><span
                            class="hl-brackets">[</span><span class="hl-number">1</span><span
                            class="hl-code">, </span><span class="hl-number">2</span><span
                            class="hl-code">, </span><span class="hl-number">3</span><span
                            class="hl-brackets">]</span><span class="hl-code">
&gt;&gt;&gt; </span><span class="hl-identifier">b</span><span class="hl-code"> = </span><span
                            class="hl-identifier">a</span><span class="hl-code">
&gt;&gt;&gt; </span><span class="hl-identifier">b</span><span class="hl-code"> </span><span
                            class="hl-reserved">is</span><span class="hl-code"> </span><span
                            class="hl-identifier">a</span><span class="hl-code">
</span><span class="hl-reserved">True</span><span class="hl-code">
&gt;&gt;&gt; </span><span class="hl-identifier">b</span><span class="hl-code"> == </span><span
                            class="hl-identifier">a</span><span class="hl-code">
</span><span class="hl-reserved">True</span><span class="hl-code">
&gt;&gt;&gt; </span><span class="hl-identifier">b</span><span class="hl-code"> = </span><span
                            class="hl-identifier">a</span><span class="hl-brackets">[</span><span
                            class="hl-code">:</span><span class="hl-brackets">]</span><span class="hl-code">
&gt;&gt;&gt; </span><span class="hl-identifier">b</span><span class="hl-code"> </span><span
                            class="hl-reserved">is</span><span class="hl-code"> </span><span
                            class="hl-identifier">a</span><span class="hl-code">
</span><span class="hl-reserved">False</span><span class="hl-code">
&gt;&gt;&gt; </span><span class="hl-identifier">b</span><span class="hl-code"> == </span><span
                            class="hl-identifier">a</span><span class="hl-code">
</span><span class="hl-reserved">True</span></pre>
                </div>
            </div>
        </blockquote>

        <h2 id="ysf8">Python运算符优先级</h2>
        <p>以下表格列出了从最高到最低优先级的所有运算符：</p>
        <table class="reference">
            <tbody>
            <tr>
                <th>运算符</th>
                <th>描述</th>
            </tr>
            <tr>
                <td>**</td>
                <td>指数 (最高优先级)</td>
            </tr>
            <tr>
                <td>~ + -</td>
                <td>按位翻转, 一元加号和减号 (最后两个的方法名为 +@ 和 -@)</td>
            </tr>
            <tr>
                <td>* / % //</td>
                <td>乘，除，取模和取整除</td>
            </tr>
            <tr>
                <td>+ -</td>
                <td>加法减法</td>
            </tr>
            <tr>
                <td>&gt;&gt; &lt;&lt;</td>
                <td>右移，左移运算符</td>
            </tr>
            <tr>
                <td>&amp;</td>
                <td>位 'AND'</td>
            </tr>
            <tr>
                <td>^ |</td>
                <td>位运算符</td>
            </tr>
            <tr>
                <td>&lt;= &lt; &gt; &gt;=</td>
                <td>比较运算符</td>
            </tr>
            <tr>
                <td>&lt;&gt; == !=</td>
                <td>等于运算符</td>
            </tr>
            <tr>
                <td>= %= /= //= -= += *= **=</td>
                <td>赋值运算符</td>
            </tr>
            <tr>
                <td>is is not</td>
                <td>身份运算符</td>
            </tr>
            <tr>
                <td>in not in</td>
                <td>成员运算符</td>
            </tr>
            <tr>
                <td>not or and</td>
                <td>逻辑运算符</td>
            </tr>
            </tbody>
        </table>

        <p>以下实例演示了Python所有运算符优先级的操作：</p>
        <div class="example">
            <div class="example">实例(Python 3.0+)</div>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-comment">#!/usr/bin/python3</span><span class="hl-code">

</span><span class="hl-identifier">a</span><span class="hl-code"> = </span><span class="hl-number">20</span><span
                        class="hl-code">
</span><span class="hl-identifier">b</span><span class="hl-code"> = </span><span class="hl-number">10</span><span
                        class="hl-code">
</span><span class="hl-identifier">c</span><span class="hl-code"> = </span><span class="hl-number">15</span><span
                        class="hl-code">
</span><span class="hl-identifier">d</span><span class="hl-code"> = </span><span class="hl-number">5</span><span
                        class="hl-code">
</span><span class="hl-identifier">e</span><span class="hl-code"> = </span><span class="hl-number">0</span><span
                        class="hl-code">

</span><span class="hl-identifier">e</span><span class="hl-code"> = </span><span class="hl-brackets">(</span><span
                        class="hl-identifier">a</span><span class="hl-code"> + </span><span
                        class="hl-identifier">b</span><span class="hl-brackets">)</span><span class="hl-code"> * </span><span
                        class="hl-identifier">c</span><span class="hl-code"> / </span><span
                        class="hl-identifier">d</span><span class="hl-code">       </span><span class="hl-comment">#( 30 * 15 ) / 5</span><span
                        class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">(a + b) * c / d 运算结果为：</span><span
                        class="hl-quotes">"</span><span class="hl-code">,  </span><span
                        class="hl-identifier">e</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-identifier">e</span><span class="hl-code"> = </span><span class="hl-brackets">(</span><span
                        class="hl-brackets">(</span><span class="hl-identifier">a</span><span class="hl-code"> + </span><span
                        class="hl-identifier">b</span><span class="hl-brackets">)</span><span class="hl-code"> * </span><span
                        class="hl-identifier">c</span><span class="hl-brackets">)</span><span class="hl-code"> / </span><span
                        class="hl-identifier">d</span><span class="hl-code">     </span><span class="hl-comment"># (30 * 15 ) / 5</span><span
                        class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">((a + b) * c) / d 运算结果为：</span><span
                        class="hl-quotes">"</span><span class="hl-code">,  </span><span
                        class="hl-identifier">e</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-identifier">e</span><span class="hl-code"> = </span><span class="hl-brackets">(</span><span
                        class="hl-identifier">a</span><span class="hl-code"> + </span><span
                        class="hl-identifier">b</span><span class="hl-brackets">)</span><span class="hl-code"> * </span><span
                        class="hl-brackets">(</span><span class="hl-identifier">c</span><span class="hl-code"> / </span><span
                        class="hl-identifier">d</span><span class="hl-brackets">)</span><span
                        class="hl-code">;    </span><span class="hl-comment"># (30) * (15/5)</span><span
                        class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">(a + b) * (c / d) 运算结果为：</span><span
                        class="hl-quotes">"</span><span class="hl-code">,  </span><span
                        class="hl-identifier">e</span><span class="hl-brackets">)</span><span class="hl-code">

</span><span class="hl-identifier">e</span><span class="hl-code"> = </span><span class="hl-identifier">a</span><span
                        class="hl-code"> + </span><span class="hl-brackets">(</span><span class="hl-identifier">b</span><span
                        class="hl-code"> * </span><span class="hl-identifier">c</span><span class="hl-brackets">)</span><span
                        class="hl-code"> / </span><span class="hl-identifier">d</span><span
                        class="hl-code">;      </span><span class="hl-comment">#  20 + (150/5)</span><span
                        class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span
                        class="hl-quotes">"</span><span class="hl-string">a + (b * c) / d 运算结果为：</span><span
                        class="hl-quotes">"</span><span class="hl-code">,  </span><span
                        class="hl-identifier">e</span><span class="hl-brackets">)</span></pre>
            </div>
        </div>
        <p>以上实例输出结果：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">(</span><span class="pln">a </span><span
                class="pun">+</span><span class="pln"> b</span><span class="pun">)</span><span class="pln"> </span><span
                class="pun">*</span><span class="pln"> c </span><span class="pun">/</span><span
                class="pln"> d </span><span class="pun">运算结果为：</span><span class="pln"> </span><span
                class="lit">90.0</span><span class="pln">
</span><span class="pun">((</span><span class="pln">a </span><span class="pun">+</span><span class="pln"> b</span><span
                class="pun">)</span><span class="pln"> </span><span class="pun">*</span><span class="pln"> c</span><span
                class="pun">)</span><span class="pln"> </span><span class="pun">/</span><span
                class="pln"> d </span><span class="pun">运算结果为：</span><span class="pln"> </span><span
                class="lit">90.0</span><span class="pln">
</span><span class="pun">(</span><span class="pln">a </span><span class="pun">+</span><span class="pln"> b</span><span
                class="pun">)</span><span class="pln"> </span><span class="pun">*</span><span class="pln"> </span><span
                class="pun">(</span><span class="pln">c </span><span class="pun">/</span><span
                class="pln"> d</span><span class="pun">)</span><span class="pln"> </span><span class="pun">运算结果为：</span><span
                class="pln"> </span><span class="lit">90.0</span><span class="pln">
a </span><span class="pun">+</span><span class="pln"> </span><span class="pun">(</span><span class="pln">b </span><span
                class="pun">*</span><span class="pln"> c</span><span class="pun">)</span><span class="pln"> </span><span
                class="pun">/</span><span class="pln"> d </span><span class="pun">运算结果为：</span><span
                class="pln"> </span><span class="lit">50.0</span></pre>

    </div>
</div>
</body>
</html>
